﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Navigation;
using Microsoft.Phone.Controls;
using Microsoft.Phone.Shell;
using MoMoTransferVN_WP.utils;
using com.mservice.momo.msg;

namespace MoMoTransferVN_WP.page
{
    public partial class newSetupPage : PhoneApplicationPage, App.IMoMoMessageCome
    {
        public newSetupPage()
        {
            InitializeComponent();
        }

        protected override void OnBackKeyPress(System.ComponentModel.CancelEventArgs e)
        {
            //Do your work here
            if (NavigationService.CanGoBack)
            {
                while (NavigationService.RemoveBackEntry() != null)
                {
                    NavigationService.RemoveBackEntry();
                }
            }
            base.OnBackKeyPress(e);
        }
        protected override void OnNavigatedTo(System.Windows.Navigation.NavigationEventArgs e)
        {
            LogUtils.f("sasasasa");

            (Application.Current as MoMoTransferVN_WP.App).setCkb((App.IMoMoMessageCome)this);

        }

        protected override void OnNavigatedFrom(System.Windows.Navigation.NavigationEventArgs e)
        {
            LogUtils.f("bbbbbbbb");
        }
       
        private void btnTieptuc_Click(object sender, RoutedEventArgs e)
        {
            if (txtPhoneNumber.Text.Length == 0)
            {
                LogUtils.f("Ban chua nhap so dien thoai");
                return;
            }

            SettingUtils.saveSetting(Assets.KEY_PHONE_NUMBER, txtPhoneNumber.Text);
            long cmdIndex = App.getNextMsgId();
            MomoMessage checkPhone = MoMoReq.processCheckPhone_Exist(cmdIndex, Assets.KEY_PHONE_NUMBER);
            (Application.Current as MoMoTransferVN_WP.App).mMoMoTcpClient.sendMoMoMesage(checkPhone);

            //MomoMessage msg = MoMoReq.processGetOtp();
            //(Application.Current as MoMoTransferVN_WP.App).mMoMoTcpClient.sendMoMoMesage(msg); 

        }

        void App.IMoMoMessageCome.onMoMoMessageCome(int cmdType, long cmdIndex, byte[] cmdBody)
        {
            if (cmdType == (int)MsgType.PHONE_EXIST_REPLY)
            {

                AgentInfo phoneRep = AgentInfo.CreateBuilder().MergeFrom(cmdBody).Build();
                if (phoneRep.RegStatus.IsReged == true)
                {
                    if (!SettingUtils.readBool(Assets.KEY_FIRST_RUN))
                    {
                        SettingUtils.saveSetting(Assets.KEY_FIRST_RUN, false);
                        testPopup.txtContent2.Text = SettingUtils.readString(Assets.KEY_PHONE_NUMBER);
                        testPopup.Visibility = Visibility.Visible;
                        testPopup.btnClose.Click += (s, d) =>
                        {
                            testPopup.Visibility = Visibility.Collapsed;
                        };
                    }

                    MomoMessage msg = MoMoReq.processGetOtp();
                    (Application.Current as MoMoTransferVN_WP.App).mMoMoTcpClient.sendMoMoMesage(msg);

                }
                else
                {
                    Dispatcher.BeginInvoke(() =>
                    {
                        //this.NavigationService.Navigate(new Uri("/page/RegisterPinPage.xaml", UriKind.Relative));
                        this.NavigationService.Navigate(new Uri("/page/EnterPinPage.xaml", UriKind.Relative));
                    });
                }
            }
            else if (cmdType == (int)MsgType.GET_OTP_REPLY)
            {
                StandardReply reply = StandardReply.CreateBuilder().MergeFrom(cmdBody).Build();
                if (reply != null)
                {
                    bool result = reply.Result;
                    if (result)
                    {
                        Dispatcher.BeginInvoke(() =>
                        {
                            this.NavigationService.Navigate(new Uri("/page/SetupVetifyOtpPage.xaml", UriKind.Relative));
                        });
                    }
                    else
                    {
                        LogUtils.f("Khong lay duoc ma otp");
                    }
                }
            }
        }
        public void onDisconnectWithError(string error)
        {

        }

        private void txtPhoneNumber_MouseEnter(object sender, System.Windows.Input.MouseEventArgs e)
        {
            txtPhoneNumber.SelectAll();
            //imgDelete.Visibility = Visibility.Visible;
        }

        private void txtPhoneNumber_MouseLeave(object sender, System.Windows.Input.MouseEventArgs e)
        {
            //imgDelete.Visibility = Visibility.Collapsed;
        }

        private void imgDelete_Tap(object sender, System.Windows.Input.GestureEventArgs e)
        {
            txtPhoneNumber.Text = string.Empty;
        }

    }
}